package com.ggh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ggh.dao.RechargeMapper;
import com.ggh.entity.Recharge;
import com.ggh.entity.User;
import com.ggh.service.RechargeService;
import com.ggh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author ： jsq
 * @date ：2019/10/13 22:24
 * @description : 充值
 * @path : com.ggh.service.impl.RechargeServiceImpl
 * @modifiedBy ：
 */

 @Service
public class RechargeServiceImpl extends ServiceImpl<RechargeMapper, Recharge> implements RechargeService {
    @Autowired
   private  UserService userService;
    /**
    * @description :充值成功，等待审核
    * @param recharge
    * @return: boolean
    * @date : 2019/10/17 10:46
    * @author : jj
    * @throws
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addpay(Recharge recharge) {
        recharge.setCreatyTime(new Date());
        recharge.setStatus(1);
        boolean insert = recharge.insert();
        if (insert){
                return true;
            }
        return false;
    }
    /**
     * @description :充值记录
     * @return: boolean
     * @date : 2019/10/17 10:49
     * @author : jj
     * @throws
     */
    @Override
    public List<Recharge> findAll(){
        List<Recharge> recharges = baseMapper.selectList(new QueryWrapper<Recharge>(null));
        return  recharges;
    }
    /**
     * @description :未审核
     * @return: boolean
     * @date : 2019/10/17 10:49
     * @author : jj
     * @throws
     */
    @Override
    public List<Recharge> findSh(){

        List<Recharge> recharges = baseMapper.selectList(new QueryWrapper<Recharge>().eq("status",1));
        return  recharges;
    }
    /**
     * @description :审核
     * @param recharge
     * @return: boolean
     * @date : 2019/10/17 10:49
     * @author : jj
     * @throws
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public  boolean update(Recharge recharge){
        recharge.setStatus(2);
        recharge.setSucceeTime(new Date());
        boolean b = recharge.updateById();
        if (b){
            User byId = userService.getById(recharge.getUserid());
            if (byId!=null){
                byId.setMoney(byId.getMoney()+recharge.getChongzhiMoney());
                boolean b1 = userService.updateById(byId);
                if (b1){
                    return true;
                }
            }
        }
        return false;
    }
}
